package com.gitee.ricesugar.mall.servlets;

import javax.imageio.ImageIO;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

@WebServlet(name = "CheckServlet", value = "/check")
public class CheckServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static int width=60;
    private static int height=20;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("=======");
        HttpSession session=request.getSession();
        response.setContentType("image/jpeg");
        ServletOutputStream servletOutputStream=response.getOutputStream();

        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics graphics=image.getGraphics();

        char[] rands=generCode();

        drawRands(graphics, rands);

        graphics.dispose();

        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        ImageIO.write(image, "JPEG", byteArrayOutputStream);
        byte[] buf=byteArrayOutputStream.toByteArray();
        response.setContentLength(buf.length);
        servletOutputStream.write(buf);
        byteArrayOutputStream.close();
        servletOutputStream.close();

        session.setAttribute("checkCode", new String(rands));
    }
    private char[] generCode(){
        //生成4位随机数
        String chars="0123456789abcdefghijklmnopqrstuvwxyz";
        char[] rands=new char[4];
        for(int i=0;i<4;i++){
            int rand=(int)(Math.random()*36);
            rands[i]=chars.charAt(rand);
        }
        return rands;
    }
    private void drawRands(Graphics g,char[] rands){
        g.setColor(Color.WHITE);
        g.setFont(new Font(null, Font.ITALIC|Font.BOLD, 18));

        g.drawString(""+rands[0], 1, 17);
        g.drawString(""+rands[1], 16, 15);
        g.drawString(""+rands[2], 31, 18);
        g.drawString(""+rands[3], 46, 16);
        System.out.println(rands);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
